package action.ManagerAction;

import com.opensymphony.xwork2.ActionSupport;
import dao.CroRootDao;
import entity.CroRole;
import entity.CroRoleRoot;
import entity.CroRoot;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroRoleRootService;
import service.CroRoleService;
import service.CroRootService;
import vo.CroRoleVo;

import java.util.List;

/**
 * Created by xuweijie on 17-5-9.
 */
public class RoleAction extends ActionSupport {

    @Autowired
    private CroRoleService croRoleService;
    @Autowired
    private CroRoleRootService croRoleRootService;
    @Autowired
    private CroRootService croRootService;

    private List<CroRoleVo> roles;
    private List<CroRoot> croRoots;
    private String roleid;
    private String successResult;
    private String rolename;
    private String[] roots;
    private String roledesc;

   /*
   * 显示全部角色和权限信息
   * */
   @RequiresPermissions("user:select")
   public String showdetail(){
       croRoots=croRootService.FindAllRoot();
       roles=croRoleService.FindAllRoleVo();
       for(CroRoleVo croRoleVo:roles){
           List<CroRoleRoot> roleRoots=croRoleRootService.FindRootByRoleId(croRoleVo.getId());
          croRoleVo.setRoots(roleRoots);
       }
       return SUCCESS;
   }

   /**
    * 添加角色
    * */
   @RequiresPermissions("user:write")
   public String addrole(){
       CroRole croRole=new CroRole();
       croRole.setRolename(rolename);
       croRole.setDescription(roledesc);
       croRoleService.add(croRole);
       for(int i=0;i<roots.length;i++){
           CroRoot croRoot= croRootService.FindRootById(roots[i]);
           CroRoleRoot croRoleRoot=new CroRoleRoot();
           croRoleRoot.setCroRole(croRole);
           croRoleRoot.setCroRoot(croRoot);
           croRoleRootService.addRoleRoot(croRoleRoot);
       }
       return SUCCESS;
   }

   /*
   * 更新角色信息
   * */
   public String uodaterole(){
      CroRole croRole=croRoleService.FindRoleById(roleid);
      croRole.setRolename(rolename);
      croRole.setDescription(roledesc);
       if(croRoleService.updateRole(croRole)){
           if(croRoleRootService.delRootByRoleId(roleid)){
               for(int i=0;i<roots.length;i++){
                   CroRoot croRoot= croRootService.FindRootById(roots[i]);
                   CroRoleRoot croRoleRoot=new CroRoleRoot();
                   croRoleRoot.setCroRole(croRole);
                   croRoleRoot.setCroRoot(croRoot);
                   if(croRoleRootService.addRoleRoot(croRoleRoot)){
                       successResult="更新成功";
                   }else {
                       successResult="更新失败";
                   }
               }
           }
       }
       return SUCCESS;
   }

   /**
    * 根据id删除角色
    * */
   @RequiresPermissions("user:delete")
   public String delrole(){
       if(roleid.equals("1")){
           successResult="不可删除超级管理员";
       }else{
           if(croRoleRootService.delRootByRoleId(roleid)){
               if(croRoleService.delRoleById(roleid)){
                   successResult="删除成功！";
               }else{
                   successResult="删除失败!";
               }
           }
       }

       return SUCCESS;
   }



    public List<CroRoleVo> getRoles() {
        return roles;
    }

    public void setRoles(List<CroRoleVo> roles) {
        this.roles = roles;
    }

    public String getRoleid() {
        return roleid;
    }

    public void setRoleid(String roleid) {
        this.roleid = roleid;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public List<CroRoot> getCroRoots() {
        return croRoots;
    }

    public void setCroRoots(List<CroRoot> croRoots) {
        this.croRoots = croRoots;
    }

    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    public String[] getRoots() {
        return roots;
    }

    public void setRoots(String[] roots) {
        this.roots = roots;
    }

    public String getRoledesc() {
        return roledesc;
    }

    public void setRoledesc(String roledesc) {
        this.roledesc = roledesc;
    }
}
